<template>
    <div>
        <el-table
            :data="tableData"
            border
            style="width: 100%">
            <el-table-column
            fixed
            prop="name"
            label="名称"
            width="250">
            </el-table-column>
            <el-table-column
            prop="good_type"
            label="设备类型"
            width="220">
            </el-table-column>
            <el-table-column
            prop="describe"
            label="描述"
            width="420">
            </el-table-column>
            <el-table-column
            prop="creater_time"
            label="创建时间"
            width="310">
            </el-table-column>
            <el-table-column
            prop="update_time"
            label="更新时间"
            width="310">
            </el-table-column>
            <el-table-column
            fixed="right"
            label="操作"
            width="200">
            <template slot-scope="scope">
                <el-button @click="update_goods(scope.row.id)" type="text" size="small">编辑</el-button>
                <el-button type="text" size="small" @click="del_goods(scope.row.id)">存档</el-button>
            </template>
            </el-table-column>
        </el-table>


        <el-pagination
			  @size-change="handleSizeChange"
			  @current-change="handleCurrentChange"
			  :current-page="paginations.page_index"
			  :page-sizes="paginations.page_sizes"
			  :page-size="paginations.page_size"
			  :layout="paginations.layout"
			  :total="paginations.total">
		</el-pagination>

    </div>
</template>



<script>
import { goods_get,goods_put } from '../../api/api.js'
export default {
    data(){
        return{
            // 货物的分页
            tableData: [],
            // 所有会晤
            allTableData: [],
            paginations:{
                        page_index:1, //当前页
                        total:0, //总数
                        page_size:5, //一页显示多少
                        page_sizes:[5,10,15,20], //每页显示多少条
                        layout:'total, sizes, prev, pager, next, jumper'
                    },
            //所属公司
            com_id:'',
        }
    },
    created() {
        this.com_id=localStorage.getItem('com_id')
    },
    mounted() {
        //货物请求
        this.get_goods()
    },
    methods: {
        // 货物编辑
        update_goods(gid){
            this.$router.push('/updategoods?gid=' + JSON.stringify(gid))
        },
        // 货物存档
        del_goods(gid){
            goods_put({goods_id:gid,is_del:1}).then(res=>{
                console.log(res)
                if(res.code==200){
                    alert(res.message)
                    this.$router.go(0)
                }
            })
        },
        //获取货物
        get_goods(){
            goods_get({'com_id':this.com_id}).then(res=>{
                for(let i=0,l=res.data.length;i<l;i++){
                    if(res.data[i].is_del==0){
                        this.allTableData.push(res.data[i])
                    }
                } 
                this.setPaginations()           
            })
        },
        // 分页
         setPaginations(){
				this.paginations.total = this.allTableData.length; //数据的数量
				this.paginations.page_index = 1; //默认显示第一页
				this.paginations.page_size = 5; //每页显示多少数据
				
				//显示数据
				this.tableData = this.allTableData.filter((item,index) => {
					return index < this.paginations.page_size;
				})
			},
			handleSizeChange(page_size) {
				this.paginations.page_index = 1; //第一页
				this.paginations.page_size = page_size; //每页先显示多少数据
				this.tableData = this.allTableData.filter((item,index) => {
					return index < page_size
				})
			},
			handleCurrentChange(page){
				// 跳转页数
				//获取当前页
				let index = this.paginations.page_size * (page -1);
				//获取总数
				let allData = this.paginations.page_size * page;
				
				let tablist=[];
				for(let i = index;i<allData;i++) {
					if (this.allTableData[i]) {
						tablist.push(this.allTableData[i])
					}
					this.tableData = tablist
				}
			}
    },
}
</script>